package com.ease.concurrent.volat;

import java.util.concurrent.TimeUnit;

/**
 * Synchronized怎么做到可见性？
 * Synchronized语义规范：
 * 1、**==进入同步块之前，先清空工作内存中的共享变量，从主内中重新加载。==**
 * 2、**==解锁前必须把修改的共享变量同步回主内存==。**
 *
 * @author: chenhs
 * @date: Created in 20:30 2019/11/5
 **/
public class VisibilityDemo {
    /**状态标识*/
    private static boolean is = true;

    public static void main(String[] args) {

        VisibilityDemo demo = new VisibilityDemo();

        new Thread(()->{
            int i = 0;
            while (VisibilityDemo.is) {
                synchronized (demo){
                    i++;
                }
               // i++;
            }
            System.out.println(i);
        }).start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        VisibilityDemo.is = false;
        System.out.println("被置为false了！");

    }

}
